package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mmlc.inter.InterActivity;
import com.mmlc.inter.ViewPagerFragmentAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.home.WebViewActivity;
import com.mmlc.network.responsebean.BaseTipsResponseBean;
import com.mmlc.utils.ACache;
import com.mmlc.widget.Indicator;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/5/22.
 */

public class MyScoreActivity extends InterActivity {
    private ImageView iv_back;
    private ImageView iv_set;
    private TextView tv_title;

    private TextView tv_rule,tv_score_num;
    private Indicator indicator_v;
    private ViewPager vp_score;
    private List<Fragment> fragments;
    private List<String> titles;
    private List<Integer> types;

    private String score;

    @Override
    protected int initContentView() {
        return R.layout.activity_my_score;
    }

    @Override
    protected void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_set = (ImageView) findViewById(R.id.iv_set);
        tv_title = (TextView) findViewById(R.id.tv_title);

        tv_rule = (TextView) findViewById(R.id.tv_rule);
        tv_score_num = (TextView) findViewById(R.id.tv_score_num);

        indicator_v = (Indicator) findViewById(R.id.indicator_v);
        vp_score = (ViewPager) findViewById(R.id.vp_score);
        fragments = new ArrayList<>();
        titles = new ArrayList<>();
        types = new ArrayList<>();

        initTitles();
        initFragment(titles.size());
        initViewPager();

        vp_score.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragments));
        indicator_v.setmViewPager(vp_score, 0);

        indicator_v.setTitles(titles);
        tv_title.setText("积分明细");
        iv_set.setVisibility(View.GONE);
    }

    @Override
    protected void initEvent() {
        iv_back.setOnClickListener(this);
        tv_rule.setOnClickListener(this);
    }

    @Override
    protected void initInfo() {
        Bundle bundle = getIntent().getExtras();
        if(bundle != null) {
            score = bundle.getString("score", "0");
            tv_score_num.setText(score);
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_rule:
                BaseTipsResponseBean bean = (BaseTipsResponseBean) ACache.get(getApplicationContext()).getAsObject(BaseTipsResponseBean.KEY);
                if(bean == null || bean.data == null || TextUtils.isEmpty(bean.data.score_role)){
                    return;
                }
                Intent intent = new Intent(MyScoreActivity.this,WebViewActivity.class);
                intent.putExtra("url", bean.data.score_role);
                startActivity(intent);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void initTitles(){
        titles.add("全部");
        titles.add("收入");
        titles.add("支出");
        types.add(0);
        types.add(2);
        types.add(1);
    }

    private void initFragment(int size){
        for (int i = 0;i < size;i++){
            Fragment fragment = new MyScoreFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type", types.get(i));
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
    }

    private void initViewPager(){
        vp_score.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
